import QtQuick 2.0
import QtQuick.Dialogs 1.3
import Qt.labs.platform 1.1
import CanvasToolBarModel 1.0
import CanvasToolType 1.0
import CanvasHandler 1.0

Rectangle {
    signal toolBarChanged();

    property string showMessage: "";
    property int toolBarIndex: 0;

    Column {
        property int cellWidth: parent.width    // 指定单元格大小
        property int cellHeight: parent.width
        id: view
        anchors.fill: parent
        anchors.topMargin: (parent.height - (repeater.count + 2) * cellHeight) / 2

        Rectangle {
            id: headerItem
            width: parent.width
            height: parent.width
            anchors.bottomMargin: 10
            Text {
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                wrapMode: Text.WordWrap

                maximumLineCount: parent.width
                text: qsTr("绘制栏")
                font.family: "Microsoft Yahei"
                color: "#b1b1b1"
                font.pixelSize: 16
            }
        }

        Repeater {
            id: repeater
            model: CanvasToolBarModel
            delegate: canvasToolBar_delegate
        }

        Rectangle {
            id: footerItem
            width: parent.width
            height: parent.width
            anchors.bottomMargin: 10
            Text {
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                wrapMode: Text.WordWrap

                maximumLineCount: parent.width
                text: showMessage
                font.family: "Microsoft Yahei"
                color: "#b1b1b1"
                font.pixelSize: 16
            }
        }
    }

    Component {
        id: canvasToolBar_delegate
        Column {
            width: view.cellWidth
            height: view.cellWidth

            // 工具
            Rectangle {


                width: parent.width
                height: parent.height / 2
                border.color: "#b1b1b1"
                border.width: 0
                radius: 5

                Image {
                    id: img
                    anchors.topMargin: 3
                    anchors.bottomMargin: 3
                    anchors.verticalCenter: parent.verticalCenter
                    width: parent.width
                    height: parent.height

                    fillMode: Image.PreserveAspectFit
                    // 改成===会出现奇怪错误
                    source: toolType == CanvasHandler.currentToolType ? toolActImg : toolNorImg

                    MouseArea {
                        anchors.fill: parent
                        enabled: true
                        hoverEnabled: true

                        onEntered: { parent.parent.border.width = 1 }
                        onExited: { parent.parent.border.width = 0 }

                        onPressed: {
                            // 按下当前正在相应的相同按键 不做处理
                            if (toolType === CanvasHandler.currentToolType)
                                return;
                            // 按下了撤销键
                            if (toolType === CanvasToolType.Backout) {
                                CanvasHandler.canvasBackout();
                                img.source = toolActImg
                                CanvasHandler.sendFigure(CanvasToolType.Backout, 0, 0, 0, 0)
                                return;
                            }
                            // 清空键
                            if (toolType === CanvasToolType.Clear) {
                                img.source = toolActImg
                                CanvasHandler.canvasClear();
                                CanvasHandler.sendFigure(CanvasToolType.Clear, 0, 0, 0, 0)
                                return;
                            }
                            // 加载
                            if (toolType === CanvasToolType.Load) {
                                loadFds.open()
                                return
                            }
                            // 保存
                            if (toolType === CanvasToolType.Save) {
                                saveFds.open()
                                return
                            }

                            showMessage = toolMessage
                            switch (toolType) {
                            case CanvasToolType.Cancel:
                                CanvasHandler.cursorShape = Qt.ArrowCursor
                                break;
                            case CanvasToolType.Rectangle:
                            case CanvasToolType.Circle:
                                CanvasHandler.cursorShape = Qt.CrossCursor
                                break;
                            }
                            CanvasHandler.currentToolType = toolType
                        }

                        onReleased: {
                            if (toolType === CanvasToolType.Backout ||
                                toolType === CanvasToolType.Clear) {
                                img.source = toolNorImg
                            }
                        }
                    }
                }
            }

            // 工具下面的描述文字
            Rectangle {
                width: parent.width
                height: parent.height / 2
                Text {
                    anchors.topMargin: 2
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.horizontalCenter: parent.horizontalCenter
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.horizontalAlignment

                    text: toolName
                    font.family: "Microsoft Yahei"
                    font.pixelSize: 12
                    color: "#b1b1b1"
                }
            }


        }
    }


    FileDialog {
        id: saveFds
        title: "需要保存的路径"
        currentFile: "file:///" + "MyPGCanvas"
        fileMode: FileDialog.SaveFile
        nameFilters: ["PGCanvas (*.pgcanvas)"]
        onAccepted: {
        var dstPath=String(saveFds.currentFile)
        if(Qt.platform.os==="windows")
              dstPath=dstPath.substr(8)

        CanvasHandler.saveToFile(dstPath)
        console.log("path:",dstPath)

        }
    }

    FileDialog {
        id: loadFds
        title: "选择文件"
        currentFile: "file:///"
        fileMode: FileDialog.OpenFile
        nameFilters: ["PGCanvas (*.pgcanvas)"]
        onAccepted: {
        var dstPath=String(loadFds.currentFile)
        if(Qt.platform.os==="windows")
              dstPath=dstPath.substr(8)

        CanvasHandler.loadFromFile(dstPath)
        console.log("path:",dstPath)

        }
    }

//    }
}
